home *** CD-ROM | disk | FTP | other *** search
/ Visual Basic Controls / Visual Basic Controls.iso / vbcontrol / edgetext / clsdataa.cl_ / clsdataa.cl
Encoding:
Visual Basic class definition  |  1998-03-30  |  20.4 KB  |  550 lines

  1. VERSION 1.0 CLASS
  2. BEGIN
  3.   MultiUse = -1  'True
  4. End
  5. Attribute VB_Name = "clsDataAccess"
  6. Attribute VB_GlobalNameSpace = False
  7. Attribute VB_Creatable = True
  8. Attribute VB_PredeclaredId = False
  9. Attribute VB_Exposed = False
  10. Option Explicit
  11.  
  12. '********************************************************************************************************
  13. 'Title:     clsDataAccess
  14. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  15. 'Purpose    This class was created to wrap SQL data access for error checking and ease of programming
  16. 'Requires:  frmLogon.frm
  17. '
  18. 'This is commented because it is recommended that objError be declared global
  19. 'The reason for this is so that the DisplayFlag and writeToFile properties will
  20. 'be persistent
  21. 'Private objError as new clsError
  22. '
  23. 'It is recommended that the Database object Dbtimesheet be declared global
  24.  
  25. 'It is also recommended that the Configuration object be declared global if it is being used
  26. 'This is so that it can be persistent
  27. '**************************************************************************************
  28.  
  29. Public Success as Boolean
  30. Public ErrorCode as Double
  31. Public ErrorMessage as string
  32.  
  33.  
  34. '********************************************************************************************************
  35. 'Title:     GetNewId
  36. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  37. 'Purpose    This Function is uses a three field table called UNIQUE_ID to generate unique key numbers
  38. '           It is intended for use with databases which do not contain such a facility internally
  39. 'Parameters:The Table Name for the key, and the field name where the key is found
  40. 'Return:    sequential double type key value
  41. '********************************************************************************************************
  42. Public Function GetNewId(psTableName As String, psKeyFieldName As String) As Double
  43.  
  44. Dim lrsData As Recordset
  45. Dim lsSelect as String, ldCurVal as Double, RetryCounter as integer
  46. Dim liCount as integer, BadCount as integer, Retries as integer
  47.  
  48.     'Convert to Uppercase for consistency
  49.     psTableName = UCase(Trim(psTableName))
  50.     psKeyFieldName = UCase(Trim(psKeyFieldName))
  51.     'Try 10 Times to get a key, this is abritrary and may be adjusted up or down according to server use
  52.     Retries = 10
  53.     RetryCounter = 0
  54.     'Loop until a new entry can be inserted
  55.     Do
  56.         'Get the current Highest Entry, speed is pretty good because of the index
  57.         lsSelect = "Select Max(UNIQUE_NUMBER) from UNIQUE_ID where TABLE_NAME = '" & psTableName & "' and FIELD_NAME = '" & psKeyFieldName & "'"
  58.         Set lrsData = OpenNewRecordSet(lsSelect)
  59.         'If no entry is found then init the counter to 1; otherwise, increment the current value by 1
  60.         if Success then
  61.             If Not Isnull(lrsData(0)) then
  62.                 ldCurVal = lrsData(0) + 1
  63.             Else
  64.                 ldCurVal = 1
  65.             End If
  66.             lrsData.Close
  67.         Else
  68.             ldCurVal = 1
  69.         End If
  70.         'by attempting an insert into a table called UNIQUE_ID where the key is all three fields, TABLE_NAME+FIELD_NAME+UNIQUE_NUMBER
  71.         'only one user will be successful at a time with the insert
  72.         'You may want to establish housekeeping on this table to erase old entries, it could grow large
  73.         lsSelect = "insert into UNIQUE_ID values('" & psTableName & "', '" & psKeyFieldName & "', ldCurVal)"
  74.         dbExecute lsSelect
  75.         RetryCounter = RetryCounter + 1
  76.     Loop While Not Success and RetryCounter < Retries
  77.     if Success then
  78.         GetNewId = ldCurVal
  79.     else
  80.         GetNewId = 0
  81.     End If
  82.  
  83. End Function
  84.  
  85. '********************************************************************************************************
  86. 'Title:     OpenGlobalDatabase
  87. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  88. 'Purpose    This Function will open a database for an ODBC datasource.  It will also
  89. '           pop a login screen if the logonrequired flag is set or if the parameters
  90. '           which are provided to the function are not successful at opening the database.
  91. '           By default, it uses the configuration class DataSource, UserId and Password
  92. '           properties for the sign on information.
  93. 'Parameters:Database to be opened, and logon required flag
  94. 'Return:    True on Success, False on Fail
  95. '********************************************************************************************************
  96. Public Function OpenGlobalDB(Dbtimesheet as Database, pLogonRequired As Integer)
  97.  
  98. Dim RetCode As Integer
  99.  
  100.     Success = False
  101.     ErrorCode = False
  102.     RetCode = True
  103.     
  104.     'if the required flag is set then
  105.     'display the logon screen
  106.     If pLogonRequired Then
  107.         Screen.MousePointer = vbNormal
  108.         frmLogon.txtLogonName = objConfiguration.LogonName
  109.         frmLogon.txtDataSource = Objconfiguration.datasource
  110.         
  111.         frmLogon.Show vbModal
  112.         If frmLogon.Cancel Then
  113.             OpenGlobalDB = False
  114.             Exit Function
  115.         End If
  116.         
  117.         Screen.MousePointer = vbHourglass
  118.         Objconfiguration.datasource = frmLogon.txtDataSource
  119.         objConfiguration.LogonName = frmLogon.txtLogonName
  120.         objConfiguration.Password = frmLogon.txtPassword
  121.     End If
  122.  
  123.     On Error goto OpenFCErrors
  124.     'loop until connection or user abort
  125.     Do While Not Success
  126.         Success = True
  127.         'Try to Open the Database
  128.         Set Dbtimesheet = OpenDatabase(Objconfiguration.datasource)
  129.         If Not RetCode Then
  130.             'this shows the user pressed cancel on the Logon form
  131.             'just get out
  132.             OpenGlobalDB = False
  133.             Exit Function
  134.         End If
  135.         If Success Then
  136.             Exit Do
  137.         End If
  138.     Loop
  139.     
  140.     OpenGlobalDB = Success
  141.     On Error GoTo 0
  142.     If Not Success Then
  143.         Exit Function
  144.     End If
  145.     
  146.     Exit Function
  147.  
  148. OpenFCErrors:
  149.     'flag the above code
  150.     Success = False
  151.     ErrorCode = Err
  152.     Screen.MousePointer = vbNormal
  153.     'ask the user for the database path
  154.     frmLogon.txtDataSource = Objconfiguration.datasource
  155.     frmLogon.txtLogonName = objConfiguration.LogonName
  156.     frmLogon.txtPassword = objConfiguration.Password
  157.     frmLogon.Cancel = False
  158.     Screen.MousePointer = vbNormal
  159.     frmLogon.Show vbModal
  160.     If frmLogon.Cancel = True Then
  161.         'if the user cancels the action, return a successful code
  162.         'and no error code, but exit the function and put a false on the function return
  163.         Success = True
  164.         ErrorCode = False
  165.         RetCode = False
  166.     End If
  167.     Objconfiguration.datasource = frmLogon.txtDataSource
  168.     objConfiguration.LogonName = frmLogon.txtLogonName
  169.     objConfiguration.Password = frmLogon.txtPassword
  170.     'Return
  171.     Resume Next
  172.     
  173. End Function
  174.  
  175. '********************************************************************************************************
  176. 'Title:     OpenRecordSetWithRecs
  177. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  178. 'Purpose    This Function will open recordset using the SQL passed into the function.
  179. '           if all goes well it returns a valid recordset, if it is not successful or
  180. '           there are no records in the recordset then it sets the recordset to NOTHING
  181. '           so that the programmer can simultaneously check for records or errors by seeing if it is set to NOTHING
  182. 'Parameters:SQL to be used in the recordset
  183. 'Return:    valid recordset on success or recs found, NOTHING on failure or no recs
  184. '********************************************************************************************************
  185. Public Function OpenRecordSetWithRecs(psSQL As String) As Recordset
  186.     
  187. Dim lrsData As Recordset
  188. Dim liCount as integer, BadCount as integer, lsSelect as String
  189.  
  190.     'The Success flag gets initialized to True and set to false if a trappable error occurs
  191.     Success = True
  192.     'The ErrorCode is the Err returned by VB for the Trapped Error
  193.     ErrorCode = False
  194.     'The DebugFlag is the provision which turns off all error checking in the table class when false
  195.     If Not objConfiguration.DebugFlag Then
  196.         On Error GoTo NoDataAccessOpenRecordSetWithRecs
  197.     End If
  198.     
  199.     lsSelect = psSQL
  200.     'Execute the SQL
  201.     Set lrsData = Dbtimesheet.OpenRecordSet(lsSelect, dbOpenSnapShot)
  202.     
  203.     'if we were able to open the recordset, but there are no recs, just set it to nothing anyway
  204.     'the success flag will remain true and error code false
  205.     If Success Then
  206.         If lrsData.RecordCount = 0 Then
  207.             Set OpenRecordSetWithRecs = Nothing
  208.         End If
  209.     End If
  210.     Set OpenRecordSetWithRecs = lrsData
  211.     On Error GoTo 0
  212.     Exit Function
  213.  
  214. NoDataAccessOpenRecordSetWithRecs:
  215.  
  216.     Success = False
  217.     ErrorCode = Err
  218.     objError.ErrorCode = Err
  219.     objError.FunctionName = "clsDataAccess.OpenRecordSetWithRecs"
  220.     If Err = 3146 then
  221.         objError.Message = "DataAccess, OpenRecordSetWithRecs " & vbcrlf & Errors(0) & " "
  222.         ErrorMessage = Errors(0)
  223.     Else
  224.         objError.Message = "DataAccess, OpenRecordSetWithRecs "
  225.         ErrorMessage = Error(Err)
  226.     End If
  227.     objError.SQL = lsSelect
  228.     objError.Display vbExclamation
  229.     Resume Next
  230.  
  231.  
  232. End Function
  233.  
  234. '********************************************************************************************************
  235. 'Title:     OpenNewRecordSet
  236. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  237. 'Purpose    This Function will open recordset using the SQL passed into the function.
  238. '           This is similar to the OpenRecordsetWithRecs, but it only returns NOTHING
  239. '           when there is an error.  It will return a valid empty recordset if no
  240. '           records are found
  241. 'Parameters:SQL to be used in the recordset
  242. 'Return:    valid recordset on success, NOTHING on failure
  243. '********************************************************************************************************
  244. Public Function OpenNewRecordSet(psSQL As String) As Recordset
  245.  
  246. Dim lrsData As Recordset
  247. Dim liCount as integer, BadCount as integer, lsSelect as String
  248.  
  249.     'The Success flag gets initialized to True and set to false if a trappable error occurs
  250.     Success = True
  251.     'The ErrorCode is the Err returned by VB for the Trapped Error
  252.     ErrorCode = False
  253.     'The DebugFlag is the provision which turns off all error checking in the table class when false
  254.     If Not objConfiguration.DebugFlag Then
  255.         On Error GoTo NoDataAccessOpenNewRecordSet
  256.     End If
  257.  
  258.     lsSelect = psSQL
  259.     'Execute the SQL
  260.     Set lrsData = Dbtimesheet.OpenRecordSet(lsSelect, dbOpenSnapShot)
  261.  
  262.     Set OpenNewRecordSet = lrsData
  263.     On Error GoTo 0
  264.     Exit Function
  265.  
  266. NoDataAccessOpenNewRecordSet:
  267.  
  268.     Success = False
  269.     ErrorCode = Err
  270.     objError.ErrorCode = Err
  271.     objError.FunctionName = "clsDataAccess.OpenNewRecordSet"
  272.     If Err = 3146 then
  273.         objError.Message = "DataAccess, OpenNewRecordSet " & vbcrlf & Errors(0) & " "
  274.         ErrorMessage = Errors(0)
  275.     Else
  276.         objError.Message = "DataAccess, OpenNewRecordSet "
  277.         ErrorMessage = Error(Err)
  278.     End If
  279.     objError.SQL = lsSelect
  280.     objError.Display vbExclamation
  281.     Resume Next
  282.  
  283.  
  284. End Function
  285.  
  286. '********************************************************************************************************
  287. 'Title:     GetFieldTypeString
  288. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  289. 'Purpose    This Function will return a string based on the microsoft field type value passed in
  290. 'Parameters:Field Type as Integer
  291. 'Return:    Field Type String to Display
  292. '********************************************************************************************************
  293. Public Function GetFieldTypeString(piFieldType As Integer) as String
  294.         
  295. Dim lsBuf As String
  296.         
  297.     Success = True
  298.     ErrorCode = False
  299.     Select Case piFieldType
  300.         Case dbBoolean
  301.             lsBuf = "Boolean"
  302.         Case dbByte
  303.             lsBuf = "Byte"
  304.         Case dbInteger
  305.             lsBuf = "Integer"
  306.         Case dbLong
  307.             lsBuf = "Long"
  308.         Case dbCurrency
  309.             lsBuf = "Currency"
  310.         Case dbSingle
  311.             lsBuf = "Single"
  312.         Case dbDouble
  313.             lsBuf = "Double"
  314.         Case dbDate
  315.             lsBuf = "Date/Time"
  316.         Case 9
  317.             lsBuf = "Reserved 9"
  318.         Case dbText
  319.             lsBuf = "Text"
  320.         Case dbBinary
  321.             lsBuf = "Binary"
  322.         Case dbMemo
  323.             lsBuf = "Memo"
  324.         Case Else
  325.             lsBuf = "Unknown"
  326.     End Select
  327.     GetFieldTypeString = lsBuf
  328.  
  329. End Function
  330.  
  331. '********************************************************************************************************
  332. 'Title:     GetFieldTypeFromString
  333. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  334. 'Purpose    This Function will return a Microsoft field type based on the string passed in
  335. 'Parameters:Field Type as string
  336. 'Return:    Microsoft Field Type Integer
  337. '********************************************************************************************************
  338. Public Function GetFieldTypeFromString(psFieldType As String) As Integer
  339.         
  340. Dim liBuf As Integer
  341.         
  342.     Success = True
  343.     ErrorCode = False
  344.     Select Case UCase(Trim(psFieldType))
  345.         Case "BOOLEAN"
  346.             liBuf = dbBoolean
  347.         Case "BYTE"
  348.             liBuf = dbByte
  349.         Case "INTEGER"
  350.             liBuf = dbInteger
  351.         Case "LONG"
  352.             liBuf = dbLong
  353.         Case "CURRENCY"
  354.             liBuf = dbCurrency
  355.         Case "SINGLE"
  356.             liBuf = dbSingle
  357.         Case "DOUBLE"
  358.             liBuf = dbDouble
  359.         Case "DATE","DATE/TIME"
  360.             liBuf = dbDate
  361.         Case "RESERVED 9"
  362.             liBuf = 9
  363.         Case "TEXT","STRING"
  364.             liBuf = dbText
  365.         Case "BINARY"
  366.             liBuf = dbBinary
  367.         Case "MEMO"
  368.             liBuf = dbMemo
  369.         Case Else
  370.             liBuf = True
  371.     End Select
  372.     GetFieldTypeFromString = liBuf
  373.  
  374. End Function
  375.  
  376. '********************************************************************************************************
  377. 'Title:     dbExecute
  378. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  379. 'Purpose    This Sub will Execute an SQL statement which is passed in
  380. 'Parameters:SQL statement to execute
  381. 'Return:    Success property is set to true if sucessful false if unsuccessful
  382. '********************************************************************************************************
  383. Public Sub dbExecute(psSQL As String)
  384.         
  385. Dim liCount as integer,BadCount as integer, lsSelect as String
  386.  
  387.     'The Success flag gets initialized to True and set to false if a trappable error occurs
  388.     Success = True
  389.     'The ErrorCode is the Err returned by VB for the Trapped Error
  390.     ErrorCode = False
  391.     'The DebugFlag is the provision which turns off all error checking in the table class when false
  392.     If Not objConfiguration.DebugFlag Then
  393.         On Error GoTo NoDataAccessdbExecute
  394.     End If
  395.     Dbtimesheet.Execute psSQL
  396.     On Error GoTo 0
  397.     Exit Sub
  398.  
  399.     lsSelect = psSQL
  400. NoDataAccessdbExecute:
  401.  
  402.     Success = False
  403.     ErrorCode = Err
  404.     objError.ErrorCode = Err
  405.     objError.FunctionName = "clsDataAccess.dbExecute"
  406.     If Err = 3146 then
  407.         objError.Message = "DataAccess, dbExecute " & vbcrlf & Errors(0) & " "
  408.         ErrorMessage = Errors(0)
  409.     Else
  410.         objError.Message = "DataAccess, dbExecute "
  411.         ErrorMessage = Error(Err)
  412.     End If
  413.     objError.SQL = lsSelect
  414.     objError.Display vbExclamation
  415.     Resume Next
  416.  
  417.  
  418. End Sub
  419.  
  420. '********************************************************************************************************
  421. 'Title:     SearchandDouble
  422. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  423. 'Purpose:   This Function will look for any single quotes in a string passed to it
  424. '           and double them for SQL compatibility
  425. 'Parameters:string to be modified
  426. 'Return:    the modified string
  427. '********************************************************************************************************
  428. Public Function SearchandDouble(lsBuf As String) As String
  429.  
  430. Dim liStrLen As Integer
  431. Dim liCurChar As Integer
  432. Dim liQuotePos As Integer
  433. Dim lsQuote As String
  434. Dim lsOutBuf As String
  435.  
  436.     lsQuote = "'"
  437.     liCurChar = 1
  438.     lsOutBuf = ""
  439.     
  440.     
  441.     liQuotePos = InStr(liCurChar, lsBuf, lsQuote)
  442.     If liQuotePos = 0 Then
  443.         lsOutBuf = lsBuf
  444.     Else
  445.         liStrLen = Len(lsBuf)
  446.         Do While liQuotePos > 0
  447.             lsOutBuf = lsOutBuf & Mid(lsBuf, liCurChar, liQuotePos - liCurChar + 1) & lsQuote
  448.             liCurChar = liQuotePos + 1
  449.             liQuotePos = InStr(liCurChar, lsBuf, lsQuote)
  450.         Loop
  451.         lsOutBuf = lsOutBuf & Mid(lsBuf, liCurChar, liStrLen)
  452.     End If
  453.  
  454.     SearchandDouble = lsOutBuf
  455.  
  456. End Function
  457. '********************************************************************************************************
  458. 'Title:     GetSingleField
  459. 'Author:    DesignGrid by W. David Ewing, Copyright 1998
  460. 'Purpose:   This method gets a single field from the database with the SQL passed in
  461. 'Parameters:SQL to execute
  462. 'Return:    return value
  463. '********************************************************************************************************
  464. Public Function GetSingleField(psSQL As String) As Variant
  465.  
  466. Dim lrsData As Recordset
  467. Dim liCount As Integer, lsSelect as string,BadCount as Integer
  468.  
  469.  
  470.     'The Success flag gets initialized to True and set to false if a trappable error occurs
  471.     Success = True
  472.     'The ErrorCode is the Err returned by VB for the Trapped Error
  473.     ErrorCode = False
  474.     'The DebugFlag is the provision which turns off all error checking in the table class when false
  475.     If Not objConfiguration.DebugFlag Then
  476.         On Error GoTo NoDataAccessGetSingleField
  477.     End If
  478.     
  479.     lsSelect = psSQL
  480.     'Execute the SQL
  481.     Set lrsData = Dbtimesheet.OpenRecordSet(lsSelect, dbOpenSnapShot)
  482.     
  483.     'if we were able to open the recordset, but there are no recs, just set it to nothing anyway
  484.     'the success flag will remain true and error code false
  485.     If Success Then
  486.         'check the field type and return the appropriate data
  487.         If lrsData.RecordCount = 0 Then
  488.             If lrsData(0).Type = dao.dbText Or lrsData(0).Type = dao.dbDate Or _
  489.                lrsData(0).Type = dao.dbTime Or lrsData(0).Type = dao.dbChar _
  490.                Or lrsData(0).Type = dao.dbMemo Then
  491.                'see if it is a date
  492.                If lrsData(0).Type = dbDate Then
  493.                   'This check is here in case you would like to have this method return a default date such as 01/01/1800
  494.                   GetSingleField = ""
  495.                Else
  496.                   GetSingleField = ""
  497.                End If
  498.             Else
  499.                GetSingleField = 0
  500.             End If
  501.         Else
  502.             'check the field type and return the appropriate data
  503.             If lrsData(0).Type = dao.dbText Or lrsData(0).Type = dao.dbDate Or _
  504.                lrsData(0).Type = dao.dbTime Or lrsData(0).Type = dao.dbChar _
  505.                Or lrsData(0).Type = dao.dbMemo Then
  506.                'see if this is a date
  507.                If lrsData(0).Type = dbDate Then
  508.                   'see if there is a time of day required
  509.                   If not isnull(lrsData(0)) then
  510.                      If CDate(Format(lrsData(0), "mm/dd/yyyy hh:mm:ss")) - CDate(Format(lrsData(0), "mm/dd/yyyy")) = 0 Then
  511.                         'no need to show time of day
  512.                         GetSingleField = Format(lrsData(0), "mm/dd/yyyy")
  513.                      Else
  514.                         GetSingleField = Format(lrsData(0), "mm/dd/yyyy hh:mm:ss")
  515.                      End If
  516.                   Else
  517.                       GetSingleField = ""
  518.                   End If
  519.                Else
  520.                   GetSingleField = lrsData(0) & ""
  521.                End If
  522.             Else
  523.                GetSingleField = Val(lrsData(0) & "")
  524.             End If
  525.             lrsData.Close
  526.         End If
  527.     Else
  528.         GetSingleField = ""
  529.     End If
  530.     On Error GoTo 0
  531.     Exit Function
  532. NoDataAccessGetSingleField:
  533.  
  534.     Success = False
  535.     ErrorCode = Err
  536.     objError.ErrorCode = Err
  537.     objError.FunctionName = "clsDataAccess.GetSingleField"
  538.     If Err = 3146 then
  539.         objError.Message = "DataAccess, GetSingleField " & vbcrlf & Errors(0) & " "
  540.         ErrorMessage = Errors(0)
  541.     Else
  542.         objError.Message = "DataAccess, GetSingleField "
  543.         ErrorMessage = Error(Err)
  544.     End If
  545.     objError.SQL = lsSelect
  546.     objError.Display vbExclamation
  547.     Resume Next
  548.  
  549. End Function
  550.